package org.nch.videoLauncher.view;

import com.jgoodies.forms.layout.CellConstraints;
import org.nch.videoLauncher.presenter.*;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Nov 16, 2008
 * Time: 9:00:00 AM
 * To change this template use File | Settings | File Templates.
 */
public class MainWindowView implements IMainWindowView {
    public JPanel mainWindowPanel;
    private JPanel topPanel;
    private JPanel viewJPanel;
    private JPanel bottomPanel;
    private JTextArea messageText;
    private JLabel titleLabel;
    private JLabel profileLabel;
    private JSpinner profileSpinner;
    private JTextField searchboxText;
    private JLabel searchboxLabel;
    private JPanel messageTextPanel;
    private MainWindowPresenter presenter;
    private static JFrame frame;

    private NavButtonView navButtonView;
    private BrowseVideoView browseVideoView;
    private AddEditVideoView addEditVideoView;
    private AddEditProfileView addEditProfileView;
    private HistoryView historyView;
    private SearchView searchView;

    private SpinnerListModel spinnerModel;
    private List<String> spinnerList;

    public MainWindowView() {

        System.out.println("OS detected: " + System.getProperty("os.name"));

        titleLabel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent event) {
                setNavButtonView();
            }
        });

        getMainWindow().addWindowListener( new WindowAdapter(){
            public void windowClosing(WindowEvent we){
                onExit();
            }
        });


        profileSpinner.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent event) {
                presenter.profileSelectionChanged();
            }
        });
        searchboxText.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.quickSearch();
            }
        });
    }

    public static void main(String[] args) {

        // mac specific settings

        System.setProperty("com.apple.mrj.application.apple.menu.about.name", "VideoBrain! v1.0");

        // create the main window

        frame = new JFrame("VideoBrain! v1.0");
        MainWindowView view = new MainWindowView();
        MainWindowPresenter presenter = new MainWindowPresenter(view);
        view.setPresenter(presenter);
        
        frame.setContentPane(view.mainWindowPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        presenter.initialize();
    }

    public void setPresenter(MainWindowPresenter mainWindowPresenter) {

        this.presenter = mainWindowPresenter;        

    }

    public void setTitleText(String title) {

        titleLabel.setText(title);

    }

    public void setNavButtonView() {

        if (navButtonView == null){
            navButtonView = new NavButtonView();
            NavButtonPresenter navButtonPresenter = new NavButtonPresenter(navButtonView, presenter);
            navButtonView.setPresenter(navButtonPresenter);
        }
        setContentPanel(navButtonView);        
    }

    public void setAddEditVideoView() {

        browseVideoView = null;
        
        if (addEditVideoView == null){
            addEditVideoView = new AddEditVideoView();
            AddEditVideoPresenter addEditVideoPresenter = new AddEditVideoPresenter(addEditVideoView, presenter);
            addEditVideoView.setPresenter(addEditVideoPresenter);
        }
        setContentPanel(addEditVideoView);
    }

    public void setAddEditProfileView() {

        if (addEditProfileView == null){
            addEditProfileView = new AddEditProfileView();
            AddEditProfilePresenter addEditProfilePresenter = new AddEditProfilePresenter(addEditProfileView, presenter);
            addEditProfileView.setPresenter(addEditProfilePresenter);
        }
        setContentPanel(addEditProfileView);
    }

    public void setBrowseVideoView() {

        if (browseVideoView == null){
            browseVideoView = new BrowseVideoView();
            BrowseVideoPresenter browseVideoPresenter = new BrowseVideoPresenter(browseVideoView, presenter);
            browseVideoView.setPresenter(browseVideoPresenter);
        }
        setContentPanel(browseVideoView);
    }

    public void setHistoryView() {

        historyView = null;
        historyView = new HistoryView();
        HistoryPresenter historyPresenter = new HistoryPresenter(historyView, presenter);
        historyView.setPresenter(historyPresenter);
        setContentPanel(historyView);
    }

    public void setSearchView() {

        if (searchView == null){
            searchView = new SearchView();
            SearchPresenter searchPresenter = new SearchPresenter(searchView, presenter);
            searchView.setPresenter(searchPresenter);            
        }
        setContentPanel(searchView);
    }

    public void setSearchView(String searchTerm) {

        if (searchView == null){
            searchView = new SearchView();
            SearchPresenter searchPresenter = new SearchPresenter(searchView, presenter);
            searchView.setPresenter(searchPresenter);
        }
        setContentPanel(searchView);
        searchView.setQuickSearchText(searchTerm);
        searchView.quickSearch();

    }

    public void setProfileList(List<String> profileNames) {
        spinnerModel = new SpinnerListModel();
        spinnerModel.setList(profileNames);
        profileSpinner.setModel(spinnerModel);
        profileSpinner.setEditor(new JSpinner.ListEditor(profileSpinner));
    }

    public void setSelectedProfile(String name) {
        spinnerModel.setValue(name);
    }

    public String getSelectedProfile() {
        return (String) profileSpinner.getModel().getValue();
    }

    public void clearProfileList() {
        spinnerList.clear();
    }

    public void setMessageContainerText(String message) {

        messageText.setText(message);
    }

    public String getMessageContainerText() {

        return messageText.getText();
    }

    public void setContentPanel(IContentPanel contentPanel) {

        viewJPanel.removeAll();
        CellConstraints cc = new CellConstraints();
        viewJPanel.add(contentPanel.getRootComponent(), cc.xy(1,1));
        viewJPanel.updateUI();    // tell (jgoodies) formlayout to refresh the view

    }

    public JFrame getMainWindow(){
        return frame;
    }

    public void onExit(){
        presenter.onExit();
    }

    public String getSearchText() {
        return searchboxText.getText();
    }
}
